//
//  ADCellViewController.m
//  Camera
//
//  Created by gml on 2018/10/23.
//  Copyright © 2018年 gml. All rights reserved.
//

#import "ADCellViewController.h"
#import "ADTableViewCell.h"

@interface ADCellViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;

@end

@implementation ADCellViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 10 || indexPath.row == 50) {
        return 200;
    }
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 10 || indexPath.row == 50) {
        ADTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ADTableViewCell"];
        cell.imageWidhConstraint.constant = [UIScreen mainScreen].bounds.size.width;
        cell.imageHeightConstraint.constant = [UIScreen mainScreen].bounds.size.height;
        cell.bgImge.image = [UIImage imageNamed:@"huihui.jpg"];
        [cell.scrollView setContentOffset:CGPointMake(0, cell.imageHeightConstraint.constant-200)];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
         cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
        return cell;
    }
}
- (void)scrollViewDidScroll:(UITableView *)scrollView
{
    NSArray *visCells = scrollView.visibleCells;
    [visCells enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[ADTableViewCell class]]) {
            ADTableViewCell *adCell = obj;
            CGFloat maxOff = adCell.imageHeightConstraint.constant - 200;
            CGFloat scrollOff = scrollView.contentOffset.y - adCell.frame.origin.y + [UIScreen mainScreen].bounds.size.height - 200 ;
            if (maxOff <= scrollOff) {
                [adCell.scrollView setContentOffset:CGPointMake(0,0)];
            }else if (scrollOff < 0){
                [adCell.scrollView setContentOffset:CGPointMake(0,maxOff)];
                
            } else{
                [adCell.scrollView setContentOffset:CGPointMake(0,(maxOff - scrollOff))];
            }
        }
    }];
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.origin.y+self.navigationController.navigationBar.bounds.size.height, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-self.navigationController.navigationBar.bounds.size.height ) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ADTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"ADTableViewCell"];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
        
    }
    return _tableView;
}



@end
